package com.abigdreamer.im.server.ui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

import com.abigdreamer.im.server.module.EIMServer;
import com.abigdreamer.im.server.sound.SoundPlay;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;

/**
 * 加载系统的界面，具体工作如下：
 * 
 * 1：初始化EIM系统主界面 2：初始化EIM的桌面右下角的系统托盘类 3：初始化服务器配置信息 4：初始化配置信息 数据库
 */
public class LoadingFrame extends BaseFrame implements Runnable {

	/**
	 */
	private static final long serialVersionUID = 1L;
	/** 加载进度条 */
	private JProgressBar jProgressBar1 = new JProgressBar();
	/** 加载页面 */
	private JLabel indexLabel = new JLabel();

	public LoadingFrame() {

		indexLabel.setText("indexLabel");
		Icon icon = ResourseMannager.getIcon("Index.jpg");
		indexLabel.setIcon(icon);

		jProgressBar1.setBorder(null);
		jProgressBar1.setStringPainted(true);
		jProgressBar1.setBorderPainted(false);
		jProgressBar1.setString("系统加栽中......请稍等！．．");
		jProgressBar1.setBackground(Color.white);
		jProgressBar1.setForeground(new java.awt.Color(102, 204, 255));
		// 368*224

		this.getContentPane().add(indexLabel, BorderLayout.NORTH);
		this.getContentPane().add(jProgressBar1, BorderLayout.SOUTH);

		this.setSize(icon.getIconWidth(), icon.getIconHeight() + 22);
		this.setUndecorated(true);
		this.setAlwaysOnTop(true);

	}

	public void setProgressText(String info) {
		jProgressBar1.setString(info);
	}

	public void run() {
		try {
			ServerMainFrame mainFrame = ServerMainFrame.getInstance();
			setProgressText("初始化服务器主界面.....");
			for (int i = 0; i < 10; i++) {
				Thread.currentThread().sleep(20);
				jProgressBar1.setValue(jProgressBar1.getValue() + 1);
			}
			EIMServer sever = EIMServer.getInstance();
			setProgressText("初始化服务器主后台系统.....");
			for (int i = 10; i < 70; i++) {
				Thread.currentThread().sleep(20);
				jProgressBar1.setValue(jProgressBar1.getValue() + 1);
			}
			EIMTrayIcon deskTopIcon = EIMTrayIcon.getInstanse();
			setProgressText("初始化服务器托盘.....");
			for (int i = 70; i < 80; i++) {
				Thread.currentThread().sleep(20);
				jProgressBar1.setValue(jProgressBar1.getValue() + 1);
			}

			setProgressText("初始化服务配置信息.....");
			for (int i = 80; i < 101; i++) {
				Thread.currentThread().sleep(20);
				jProgressBar1.setValue(jProgressBar1.getValue() + 1);
			}
			this.dispose();
			Thread deskTopIconThread = new Thread(deskTopIcon);
			deskTopIconThread.start();
			Thread mainFrameThread = new Thread(mainFrame);
			mainFrameThread.start();
			SoundPlay.playStartSound();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		LoadingFrame loadingFrame = new LoadingFrame();
		loadingFrame.setLocationRelativeTo(null);
		loadingFrame.setVisible(true);
	}
}
